package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsStorageMapper;
import com.qf.pojo.DtsStorage;
import com.qf.service.DtsStorageService;
import com.qf.util.AliyunOSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.UUID;

/**
 * @version v1.0.0
 * @Description:
 * @author: 姜红凯
 * @date: 2022.02.23
 * @since 1.0
 */
@Service
public class DtsStorageServiceImpl implements DtsStorageService {

    @Value("${aliyun.oss.endpoint}")
    private String endpoint;
    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;

    @Autowired
    DtsStorageMapper storageMapper;
    /**
     * 上传文件到 阿里云OSS
     * @return
     */
    @Override
    public Object upload(String filename,byte[] contents,int size) throws Exception {

        String uuid = UUID.randomUUID().toString();

        String url = AliyunOSSUtil.uploadImage(endpoint, accessKeyId, accessKeySecret, bucketName, uuid + filename, contents);

        String ext = filename.substring(filename.lastIndexOf("."));

        DtsStorage storage = new DtsStorage();
        //上传到阿里云oss上的路径
        storage.setUrl(url);
        //设置文件的后缀名
        storage.setType(ext);
        storage.setKey(uuid + filename);
        storage.setName(filename);
        storage.setSize(size);
        storage.setDeleted(false);
        storage.setAddTime(new Date());
        storage.setUpdateTime(new Date());


        storageMapper.insert(storage);

        return storage;
    }

    @Override
    public IPage<DtsStorage> queryPage(Integer page, Integer limit, String name, String key, String sort, String order) {
        IPage<DtsStorage> ipage = new Page<>(page,limit);
        QueryWrapper<DtsStorage> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(key)){
            wrapper.like("`key`",key);
        }

        if(!StringUtils.isEmpty(order) && !StringUtils.isEmpty(sort)){
            if(order.equals("desc")){
                wrapper.orderByDesc(sort);
            }
            else {
                wrapper.orderByAsc(sort);
            }
        }
        storageMapper.selectPage(ipage,wrapper);

        return ipage;
    }

    @Override
    public DtsStorage update(DtsStorage storage) {
        storage.setUpdateTime(new Date());
        storageMapper.updateById(storage);
        return storage;
    }

    @Override
    public void delete(DtsStorage storage) {

        //删除数据库数据
        storageMapper.deleteById(storage.getId());
        //删除阿里云的数据
        AliyunOSSUtil.deleteFile(endpoint,accessKeyId,accessKeySecret,bucketName,storage.getKey());
    }
}
